//
//  GGIndicatorHud.m
//  HudTest
//
//  Created by FXGuo on 2017/5/25.
//  Copyright © 2017年 guoguo. All rights reserved.
//

#import "GGIndicatorHud.h"

@interface GGIndicatorHud()

@property (weak, nonatomic) IBOutlet UIView *hudView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (nonatomic,strong) CAShapeLayer *hubLayer;
@property (nonatomic,assign) BOOL isReadyRemove;

@end

@implementation GGIndicatorHud

+ (instancetype)getHud
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([GGIndicatorHud class]) owner:nil options:nil]lastObject];
}

+ (void)showIndicatorInView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    GGIndicatorHud *hud = [GGIndicatorHud getHud];
    hud.frame = view.bounds;
    [view addSubview:hud];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = hud.iconView.bounds;
    layer.lineWidth = 2;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(32, 32) radius:32-2 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    layer.path = path.CGPath;
    [hud.iconView.layer addSublayer:layer];
    hud.hubLayer = layer;
    
    [hud beginAnimation];
}
- (void)beginAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        self.hudView.alpha = 0.8;
    } completion:nil];
    CABasicAnimation *rotaionAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    rotaionAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 0, 1)];
    rotaionAnimation.duration = 0.4;
    rotaionAnimation.repeatCount = MAXFLOAT;
    rotaionAnimation.cumulative = YES;
    [self.hubLayer addAnimation:rotaionAnimation forKey:@"animation"];
}
- (void)endAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        self.hudView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//---------------隐藏方法---------------
+ (void)hideIndicatorHud
{
    [self hideIndicatorHudInView:nil];
}
+ (void)hideIndicatorHudInView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    GGIndicatorHud * hud = [self getIndicatorHudWithView:view];
    if (hud!=nil) {
        hud.isReadyRemove = YES;
        [UIView animateWithDuration:0.5 animations:^{
            hud.hudView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [hud removeFromSuperview];
            [hud.hubLayer removeAllAnimations];
        }];
    }
}
//获取water方法
+ (GGIndicatorHud *)getIndicatorHudWithView:(UIView *)view
{
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            GGIndicatorHud *hud = (GGIndicatorHud *)subview;
            if (hud.isReadyRemove) {
                continue;
            }
            return hud;
        }
    }
    return nil;
}

@end
